﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace DevelopTool.Common
{
    public static class HotKeyHelper
    {
        private static List<string> globalHotKey = new List<string>();

        /// <summary>
        /// 根据热键集合获取符合Sunny.UI热键规则的字符串
        /// </summary>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        public static string GetSunnyUIHotKeyStr(List<int> keyValues)
        {
            List<string> modifierKeys = new List<string>();
            string keys = string.Empty;
            foreach(int keyValue in keyValues.OrderByDescending(it => it))
            {
                if(keyValue == 16)
                {
                    modifierKeys.Add("Shift");
                }
                else if(keyValue == 17)
                {
                    modifierKeys.Add("Control");
                }
                else if(keyValue == 18)
                {
                    modifierKeys.Add("Alt");
                }
                else
                {
                    keys = ((Keys)keyValue).ToString();
                }
            }

            if(modifierKeys.Count <= 0) modifierKeys.Add("None");//给个默认的辅助建

            return string.Join(", ",modifierKeys.Distinct()) + "  " + keys;//Alt, Control  J
        }


        /// <summary>
        /// 根据Sunny.UI热键字符串获取Winfrom热键字符串，仅用作显示
        /// </summary>
        /// <param name="sunnyUIHotKeyStr"></param>
        /// <returns></returns>
        public static string GetWinfromHotKeyIntoShow(string sunnyUIHotKeyStr)
        {
            if(string.IsNullOrEmpty(sunnyUIHotKeyStr) || sunnyUIHotKeyStr == "无") return sunnyUIHotKeyStr;

            string[] keyStrs = sunnyUIHotKeyStr.Split(new string[] { "  " },StringSplitOptions.None);

            if(keyStrs.Length <= 1) return sunnyUIHotKeyStr;

            string keyStr = keyStrs[1];
            string ModifierKeys = string.Empty;
            string[] modifierKeys = keyStrs[0].Split(new string[] { ", " },StringSplitOptions.None).ToArray();

            foreach(string modifierKey in modifierKeys.OrderByDescending(it => it))
            {
                if(modifierKey == "Control")
                {
                    ModifierKeys += "Ctrl+";
                }
                else
                {
                    ModifierKeys += modifierKey + "+";
                }
            }

            return (ModifierKeys + keyStr).Replace("None+",string.Empty);
        }


        /// <summary>
        /// 获取用作注册的热键
        /// </summary>
        /// <param name="sunnyUIHotKeyStr"></param>
        /// <returns></returns>
        public static Tuple<Sunny.UI.ModifierKeys,Keys> GetHotKeyAsRegister(string sunnyUIHotKeyStr)
        {
            if(string.IsNullOrEmpty(sunnyUIHotKeyStr) || sunnyUIHotKeyStr == "无") return null;

            string[] keyStrArr = sunnyUIHotKeyStr.Split(new string[] { "  " },StringSplitOptions.None);

            if(keyStrArr.Length <= 1) return null;

            Sunny.UI.ModifierKeys modifierKeys = Sunny.UI.ModifierKeys.None;

            Keys keys = Keys.None;
            if(Enum.TryParse(keyStrArr[1],out Keys key))
            {
                keys = key;
            }

            string[] modifierKeyArr = keyStrArr[0].Split(new string[] { ", " },StringSplitOptions.None).ToArray();

            foreach(string modifierKey in modifierKeyArr)
            {
                if(modifierKey == "Alt")
                {
                    modifierKeys |= Sunny.UI.ModifierKeys.Alt;
                }
                else if(modifierKey == "Control")
                {
                    modifierKeys |= Sunny.UI.ModifierKeys.Control;
                }
                else if(modifierKey == "Shift")
                {
                    modifierKeys |= Sunny.UI.ModifierKeys.Shift;
                }
                else if(modifierKey == "None")
                {
                    modifierKeys |= Sunny.UI.ModifierKeys.None;
                }
            }

            return new Tuple<Sunny.UI.ModifierKeys,Keys>(modifierKeys,keys);
        }


        /// <summary>
        /// 注册全局快捷键
        /// </summary>
        /// <param name="sunnyUIHotKeyStrList"></param>
        public static void RegisterGlobalHotKey(this UIForm uIForm,string sunnyUIHotKeyStr)
        {
            if(string.IsNullOrEmpty(sunnyUIHotKeyStr) || sunnyUIHotKeyStr == "无") return;

            Tuple<Sunny.UI.ModifierKeys,Keys> tuple = GetHotKeyAsRegister(sunnyUIHotKeyStr);
            if(tuple == null) return;

            uIForm.RegisterHotKey(tuple.Item1,tuple.Item2);

            globalHotKey.Add(sunnyUIHotKeyStr);
        }



        /// <summary>
        /// 批量卸载全局快捷键
        /// </summary>
        /// <param name="uIForm"></param>
        public static void UnRegisterGlobalHotKey(this UIForm uIForm)
        {
            foreach(var sunnyUIHotKeyStr in globalHotKey)
            {
                if(string.IsNullOrEmpty(sunnyUIHotKeyStr)) return;

                Tuple<Sunny.UI.ModifierKeys,Keys> tuple = GetHotKeyAsRegister(sunnyUIHotKeyStr);
                if(tuple == null) return;

                uIForm.UnRegisterHotKey(tuple.Item1,tuple.Item2);
            }
        }

        /// <summary>
        /// 卸载某个全局快捷键
        /// </summary>
        /// <param name="uIForm"></param>
        /// <param name="sunnyUIHotKeyStr"></param>
        public static void UnRegisterGlobalHotKey(this UIForm uIForm,string sunnyUIHotKeyStr)
        {
            if(string.IsNullOrEmpty(sunnyUIHotKeyStr) || sunnyUIHotKeyStr == "无") return;

            Tuple<Sunny.UI.ModifierKeys,Keys> tuple = GetHotKeyAsRegister(sunnyUIHotKeyStr);
            if(tuple == null) return;

            uIForm.UnRegisterHotKey(tuple.Item1,tuple.Item2);

        }

    }
}
